import 'package:amway_superapp_creator_plugin/models/sms_model.dart';
import 'package:amway_superapp_creator_plugin/net/http_manager.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

import '../api.dart';
import 'base_model.dart';

class EditPhoneModel extends BaseModel {
  EditPhoneModel({@required Api api}) : super(api: api);
  String value = ""; //手机号
  String code = ""; //验证码
  bool isPhoneError = false; //手机号错误
  bool isCodeError = false; //验证码错误
  bool isSend = false;
  int countdownNum = 0;

  void setCountdownNum(int countdownNum) {
    this.countdownNum = countdownNum;
    notifyListeners();
  }

  void change(String value) {
    this.value = value;
    notifyListeners();
  }

  void setCode(String code) {
    this.code = code;
    notifyListeners();
  }

  void phoneError(bool isPhoneError) {
    this.isPhoneError = isPhoneError;
    notifyListeners();
  }

  void send(bool send) {
    this.isSend = send;
    notifyListeners();
  }

  void codeError(bool isCodeError) {
    this.isCodeError = isCodeError;
    notifyListeners();
  }

/*
* 发送短信
* */

  void sendMsg({BoolStringCallBack back}) {
    HttpManager().get(
        url: "apis/v1/creator/index/sendMsg/$value",
        tag: null,
        successCallback: (data) {
          if (data != null) {
            var smsCode = SmsCode.fromJson(data);
            if (smsCode != null && smsCode.smsCode != null) {
              code = smsCode.smsCode;
              notifyListeners();
            }
            back.call(true, "");
          }
        },
        errorCallback: (data) {
          back.call(false, data.message);
        });
  }

  void verify({BoolStringCallBack back}) {
    HttpManager().post(
        url: "apis/v1/creator/index/checkSmsCode/$value/$code",
        tag: null,
        successCallback: (data) {
          back.call(true, "");
        },
        errorCallback: (data) {
          back.call(false, data.message);
        });
  }
}
